<?php
	/**
	 * 货品列表控制器
	 */
	 class GoodsListModel extends Model{
	 	
		public $table = 'goods_lists';
		public $validate = array(
			array('combine','nonull','请选择组合',2,3),
			array('goods_number','nonull','数量不能为空',2,3),
		);
		
		/**
		 * 获得当前商品的规格 
		 */
		 public function getGoodsAttr($gid){
		 	//获得当前商品的类型ID 
		 	$typeid = K('Goods')->where("gid={$gid}")->getField("goods_typeid");
			//获得当前商品已选择的规格
			$allSpec = M()->join('__typeattr__ t JOIN __goods_attr__ ga ON ga.typeattr_id=t.id ')->where("attrtype=1 and goods_gid={$gid}")->all();
//			var_dump($allSpec);
			//获得标准的ID
			foreach($allSpec  as $key=>$val){
				//获得所有的ID
				$st[$val['id']] = $val['id'];
			}
			$st = array();
			//重组数组 spec
			foreach($allSpec as $key=>$val){
				//如果存在一个数组中
				$st[] = $val['attrname'];
			}
			//去重
			$st = array_values(array_unique($st));
			
			$allSpec['st'] = $st;
			return $allSpec;
		 }
			
	 	/**
		 * 将数组添加到表中
		 */
		 public function addGoodsList(){
		 	$_POST['combine'] = $_POST['combine0'].','.$_POST['combine1'];
		 	if(!$this->create()) return false;
			$data = $this->data;
			//检测组合是否已经存在
			if($this->where("combine='{$data['combine']}'")->find()){
				$this->error = '组合已经存在请更换';
				return false;
			}
			$gid = $data['goods_gid'];
			//如果不存在货号那么自动生成一下货号
			if(empty($data['goods_sn'])){
				$data['goods_sn'] = 'SHOP'.time().mt_rand(0,999);
			}	
			if($this->add($data)){
				//更新商品总数量
					$goodsCount = current($this->field('sum(goods_number) as count')->where("goods_gid={$gid}")->all());
					K('Goods')->where("gid={$gid}")->update(array('goods_count'=>$goodsCount['count']));
					return true;
				}else{
					return false;
				}
			
		 }
		 
		 /**
		  * 删除相应商品所有的库存
		  */
		  public function delAllGoodsList($gid){
		  	if($this->where("goods_gid={$gid}")->delete()){
		  		return true;
		  	}else{
		  		return false;
		  	}
		  }
		 
		 /**
		 * 获得当前组合的库存数
		 */
		 public function getStock($combine){
		 	$result = $this->where("combine='{$combine}'")->find();
			return $result;
		 }
		
		 
		 
		 /**
		  * 获得当前商品的所有库存列表
		  */
		  public function getGoodsNum($gid){
		  	$goodsNum = $this->where("goods_gid={$gid}")->all();
			//将商品规格拆分为一个数组
			foreach($goodsNum as $k=>$v){
				$goodsNum[$k]['combine'] = explode(',',$v['combine']);
				$goodsNum[$k]['combinename'] = array();
				//查询相应商品规格的名称
				foreach($goodsNum[$k]['combine'] as $kk=>$vv){
					$goodsNum[$k]['combinename'][] = K('GoodsAttr')->where("attid={$vv}")->getField('attr_value'); 
				}
			}
			return $goodsNum;
		  }
		 
		 /**
		  * 更新当前的组合先 
		  * 先检测当前的组合是否存在如果存在那么返回一个错误 
		  * 不包含当前要更新的这个组合
		  */
		  public function updateCombine($data){
//		  	var_dump($data);
		  	if(!$data['newcomb']){
		  		$this->error = '请选择规格';
		  		return false;
		  	}
		  	//先查询出当前的数组
		  	$oldData = $this->where("lid={$data['lid']}")->find();
			$oldDataComb = $oldData['combine'];
			//查询新组合是否存在
			$data['newcomb'] = implode(',',$data['newcomb']);
			//查询新的组合是否存在
			if($this->where("combine NOT IN('{$oldDataComb}') AND combine='{$data['newcomb']}'")->find()){
				$this->error = '组合已经存在请更换';
				return false;
			}else{
				//重组数组	
				$newData['lid'] = $data['lid'];
				$newData['combine'] = $data['newcomb'];
				$newData['goods_sn'] = $data['newSn'];
				$newData['goods_number'] = $data['newNum'];
				//更新当条数据
				$this->save($newData);
				//先将原来货品的数量减去再加上新的数量
				K('Goods')->dec('goods_count',"gid={$oldData['goods_gid']}",$oldData['goods_number']);
				//再加上现在的数量
				K('Goods')->inc('goods_count',"gid={$oldData['goods_gid']}",$newData['goods_number']);
				//将新的值返回去		
				return $data;
			}
			
		  }
		 
		 
		/**
		 * 将一个指定的字符串按指定的字符转为数组
		 */
		 private function specArray($allSpec,$str,$field){
		 	foreach($allSpec as $k=>$v){
		 		$allSpec[$k][$field] = explode($str,$v[$field]);
		 	}
			return $allSpec;
		 }
		
		
	 }
	